<template>
  <div>
    <el-dialog class="positionDialog" :title="this.ruleForm.id ? '修改' : '新增'" :visible="showDialog" width="98%"
      :before-close="handleClose" :close-on-click-modal="false">
      <div class="content">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <div class="title fir-title">
            <svg-icon class="svg-icon" icon-class="infoAdd" />
            <span>入库过磅</span>
            <div class="button-box">
              <div :style="`background:${this.ruleForm.radio ? 'red' : '#0e7b67'}`" @click="radioClick"
                class="button-boxItem">作为新记录添加</div>
            </div>
          </div>
          <div class="form">
            <el-form-item class="activeClassInput" label="车牌号" prop="carName" style="width:500px">
              <el-input v-model="ruleForm.carName" disabled placeholder="请输入车牌号"></el-input>
              <el-button class="redAdd" style="border-color:green ;
  color:green ;" @click="getCarName">刷新</el-button>
              <el-button class="redAdd" style="width:80px"
                @click="handleShowAddDialog('editCarNumber')">更正/录入</el-button>
            </el-form-item>
          </div>
          <div class="form">
            <el-form-item label="智慧卡号" prop="">
              <el-input v-model="ruleForm.cardNumber" placeholder="请输入智慧卡号" :disabled="true"></el-input>
            </el-form-item>
            <!-- <el-form-item label="车牌号" prop="carName">
              <el-select style="width: 100%;" v-model="ruleForm.carName" filterable placeholder="请选择车牌号或输入关键词">
                <el-option v-for="item in carList" :key="item.value" :label="item.label" :value="item.label">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('carInfo')">新增</el-button>
            </el-form-item> -->
            <el-form-item label="司机姓名" :prop="'driverPhone'">
              <el-select style="width: 100%;" v-model="ruleForm.driverPhone" @change="driverNamechange" filterable
                placeholder="请选择司机姓名或输入关键词">
                <el-option v-for="item, index in driverList" :key="item.value + index" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('driverInfo')">新增</el-button>
            </el-form-item>
            <el-form-item v-if="hasIdentifier('Inbound.field')" label="押车人" prop="">
              <el-select style="width: 100%;" v-model="ruleForm.personPhone" filterable placeholder="请选择押车人或输入关键词"
                @change="handleEscortPerson">
                <el-option v-for="item, index in escortPersonList" :key="item.value + index" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('betcarInfo')">新增</el-button>
            </el-form-item>
          </div>
          <div class="title">
            <span>入库信息</span>
          </div>
          <div class="form">
            <el-form-item label="农场" prop="farmCode">
              <el-select style="width: 100%;" v-model="ruleForm.farmCode" disabled filterable placeholder="请选择农场或输入关键词"
                @change="handleSelectTeam">
                <el-option v-for="item in farmList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="来自生产队" prop="sourceTeam">
              <el-select style="width: 100%;" v-model="ruleForm.sourceTeam" filterable placeholder="请选择生产队或输入关键词"
                @change="handleGetCropType">
                <el-option v-for="item in teamList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item v-if="hasIdentifier('Inbound.field')" label="来自地块号" prop="massifNumber">
              <el-select style="width: 100%;" v-model="ruleForm.massifNumber" filterable placeholder="请选择地块号或输入关键词"
                @change="setmassifName" :disabled="ruleForm.sourceTeam ? false : true">
                <el-option v-for="item, index in massifNumberList" :key="item.value + index" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="种类" :prop="'cropType'">
              <el-select style="width: 100%;" v-model="ruleForm.cropType" filterable placeholder="请选择作物类型或输入关键词"
                @change="handleChangeCropType" @focus="handleInspect">
                <el-option v-for="item, index in cropinfoList" :key="item.label + index" :label="item.label"
                  :value="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="品质" :prop="'cropQuality'">
              <el-select style="width: 100%;" v-model="ruleForm.cropQuality" filterable placeholder="请选择作物品质或输入关键词"
                :disabled="ruleForm.cropType ? false : true" @change="handleWarehouse">
                <el-option v-for="item in cropQualityList" :key="item.value" :label="item.label" :value="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="仓号" :prop="'warehouseNumber'">
              <el-select style="width: 100%;" v-model="ruleForm.warehouseNumber" filterable placeholder="请选择仓号或输入关键词"
                :disabled="ruleForm.cropQuality ? false : true" @change="handleDuo">
                <el-option v-for="item in wareList" :key="item.value" :label="item.label" :value="item.label">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="垛号" :prop="'stampId'">
              <el-select style="width: 100%;" v-model="ruleForm.stampId" filterable placeholder="请选择垛号或输入关键词"
                :disabled="ruleForm.warehouseNumber ? false : true">
                <el-option v-for="item in duoList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="作物品种" :prop="'cropVarieties'">
              <el-select style="width: 100%;" v-model="ruleForm.cropVarieties" filterable placeholder="请选择作物品种或输入关键词"
                :disabled="ruleForm.cropType ? false : true">
                <el-option v-for="item in cropVarietiesList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('cropVariety')">新增</el-button>
            </el-form-item>
            <el-form-item label="类别" :prop="'categoryName'">
              <el-select style="width: 100%;" v-model="ruleForm.categoryName" filterable placeholder="请选择类别或输入关键词">
                <el-option v-for="item in categoryList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('cropType')">新增</el-button>
            </el-form-item>
          </div>
          <div class="title">
            <span>过磅信息</span>
            <el-checkbox style="margin-left:50px" @change="radioChange" v-model="ruleForm.radio"><span
                style="font-weight: 900;">作为新记录添加</span></el-checkbox>
          </div>
          <div class="form">
            <el-form-item label="毛重(kg)" prop="">
              <el-input class="activeClassInput" oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.gw"
                placeholder="请输入毛重" disabled @change="handleGW"></el-input>
              <el-button class="redAdd" style="border-color:green ;
                  color:green ;" @click="getTotalWeight">刷新</el-button>
            </el-form-item>
            <el-form-item label="皮重(kg)" prop="tare">
              <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.tare" placeholder="请输入皮重"
                class="activeClassInput" disabled @change="handleTARE"></el-input>
            </el-form-item>
            <el-form-item label="净重(kg)" v-if="hasIdentifier('Inbound.field')" prop="">
              <el-input v-model="netWeight" placeholder="自动计算" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="净重(kg)" v-if="!hasIdentifier('Inbound.field')" prop="nw">
              <el-input v-model="ruleForm.nw" placeholder="自动计算" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="过毛时间" :prop="ruleForm.gw ? 'hairingTime' : ''">
              <el-date-picker style="width: 100%;" v-model="ruleForm.hairingTime" format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="请选择过毛时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="过皮时间" :prop="ruleForm.tare ? 'peelingTime' : ''">
              <el-date-picker style="width: 100%;" v-model="ruleForm.peelingTime" format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="请选择过皮时间">
              </el-date-picker>
            </el-form-item>
          </div>
          <div class="title">
            <span>扦样质检信息</span>
          </div>
          <div class="form">
            <el-form-item label="含水量(%)" :prop="ruleForm.gw ? 'water' : ''">
              <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.water"
                placeholder="请输入含水量(0-100)" :disabled="ruleForm.gw ? false : true"></el-input>
            </el-form-item>
            <el-form-item label="含油量(%)" prop="">
              <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.yau"
                placeholder="当作物为油菜时请输入含油量(0-100)"
                :disabled="(ruleForm.gw && ruleForm.cropType == '油菜') ? false : true"></el-input>
            </el-form-item>
            <el-form-item label="面筋值(%)" :prop="ruleForm.gw ? '' : ''">
              <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.glutenValue"
                placeholder="当作物为小麦时请输入面筋值(0-100)"
                :disabled="(ruleForm.gw && ruleForm.cropType == '小麦') ? false : true"></el-input>
            </el-form-item>
            <el-form-item v-if="hasIdentifier('Inbound.field')" label="扣水比例(%)" prop="">
              <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.waterRatio" placeholder="请输入扣水比例"
                @input="handleWater" style="position: relative;"></el-input>
              <div class="error-message" style="position: absolute;bottom: -30px;">扣水重量：{{ waterWeight }} kg</div>
            </el-form-item>
            <el-form-item v-if="hasIdentifier('Inbound.field')" label="扣杂比例(%)" prop="">
              <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.mixedRatio" placeholder="请输入扣杂比例"
                @input="handleMis" style="position: relative;"></el-input>
              <div class="error-message" style="position: absolute;bottom: -30px;">扣杂重量：{{ mixedWeight }} kg</div>
            </el-form-item>
            <el-form-item label="扣杂后净重(kg)" v-if="hasIdentifier('Inbound.field')" prop="nw" style="position: relative;">
              <el-input v-model="ruleForm.nw" placeholder="自动计算" :disabled="true"></el-input>
              <div class="error-message" style="position: absolute;bottom: -30px;">扣杂后净重 = 净重*（1-扣水比例-扣杂比例）</div>
            </el-form-item>
          </div>
        </el-form>

      </div>
      <el-dialog width="30%" title="提示" :visible.sync="innerVisible" append-to-body :close-on-click-modal="false">
        <div style="height:100px;text-align: center;">
          您本次过磅重量与上次过磅重量相近，请确认是否关联上次过磅重量
        </div>
        <div class="dialog-footer333">
          <el-button class="submit" @click="() => { innerVisible = false; innerVisible2 = false; }">确 认</el-button>
          <!-- <el-button class="back" @click="handleClose">返 回</el-button> -->
        </div>
      </el-dialog>
      <div class="dialog-footer333">
        <el-button class="submit" @click="handleSubmit">确 认</el-button>
        <el-button class="back" @click="handleClose">返 回</el-button>
      </div>

    </el-dialog>

    <!-- 新增车牌号 -->
    <CarInfo ref="CarInfo" :showDialog.sync="carInfo" @handleRefresh="handleRefreshList('carInfo')"></CarInfo>
    <!-- 司机信息 -->
    <DriverInfo ref="DriverInfo" :showDialog.sync="driverInfo" @handleRefresh="handleRefreshList('driverInfo')">
    </DriverInfo>
    <!-- 押车人信息 -->
    <BetcarInfo ref="BetcarInfo" :showDialog.sync="betcarInfo" @handleRefresh="handleRefreshList('betcarInfo')">
    </BetcarInfo>
    <!-- 种类 -->
    <CropInfo ref="CropInfo" :showDialog.sync="cropInfo" @handleRefresh="handleRefreshList('cropInfo')"></CropInfo>
    <!-- 作物品种 -->
    <CropVariety ref="CropVariety" :showDialog.sync="cropVariety" @handleRefresh="handleRefreshList('cropVariety')">
    </CropVariety>
    <!-- 作物类别 -->
    <CropType ref="CropType" :showDialog.sync="cropType" @handleRefresh="handleRefreshList('cropType')"></CropType>
    <!-- 水份扣重 -->
    <WaterStandard ref="WaterStandard" :showDialog.sync="waterStandard"
      @handleRefresh="handleRefreshList('waterStandard')">
    </WaterStandard>
    <!-- 扣杂标准 -->
    <ImpurityStandard ref="ImpurityStandard" :showDialog.sync="impurityStandard"
      @handleRefresh="handleRefreshList('impurityStandard')"></ImpurityStandard>
    <HandlTable ref="handlTableAdd" :showDialog.sync="dialogVisible" :setTableData='addTableDate'></HandlTable>
  </div>
</template>

<script>
import '../scss/style.scss'
import rules from '@/utils/rules'
import { Message, MessageBox } from 'element-ui'
import { filterSelect, formatDate } from '@/utils/methods'
import { refreshWithApi, getPlotUnitListApi } from '@/api/inStorage'
import { addApi, editApi, warehouseApi, getCropTypeApi, getduoApi, getQualityApi, addVehicleInfo, getInboundByCarName } from '@/api/inCage'
import { selectApi, carinfoApi, driverInfoApi, cropTypeApi, categorySelectApi, escortPersonApi } from '@/api/select'
import { getCropWeight, getCarLicense, getVehicleInfo, addOrUpdate } from '@/api/table'
import CarInfo from '@/views/info/components/CarInfo/dialog/AddPopu.vue'
import DriverInfo from '@/views/info/components/DriverInfo/dialog/AddPopu.vue'
import BetcarInfo from '@/views/info/components/BetcarInfo/dialog/AddPopu.vue'
import CropInfo from '@/views/info/components/CropInfo/dialog/AddPopu.vue'
import CropVariety from '@/views/info/components/CropVariety/dialog/AddPopu.vue'
import CropType from '@/views/info/components/CropType/dialog/AddPopu.vue'
import WaterStandard from '@/views/info/components/WaterStandard/dialog/AddPopu.vue'
import ImpurityStandard from '@/views/info/components/ImpurityStandard/dialog/AddPopu.vue'
import HandlTable from '../../info/components/Clearing/editCarNumber'
import { mapGetters } from 'vuex';
export default {
  props: ['showDialog'],
  components: {
    CarInfo,
    DriverInfo,
    BetcarInfo,
    CropInfo,
    CropVariety,
    CropType,
    WaterStandard,
    ImpurityStandard,
    HandlTable
  },
  data() {
    return {
      ruleForm: {
        farmCode: this.$farmCode,
        teamCode: this.$teamCode,
        driverName: "",
        personName: "",
        sourceTeam: "",
        warehouseNumber: "",
        cropQuality: "",
        stampId: "",
        cropType: "",
        cropVarieties: "",
        categoryName: "",
        carName: '',
        waterRatio: null,
        mixedRatio: null,
      },
      // 净重
      netWeight: '',
      waterWeight: 0,
      mixedWeight: 0,
      ruleForm2: {},
      rules: {
        cropQuality: [
          { required: true, message: '请选择品质', trigger: 'blur' },
        ],
        driverPhone: [
          { required: true, message: '请选择司机名称', trigger: 'blur' },
        ],
        farmCode: [
          { required: true, message: '请选择农场', trigger: 'blur' },
        ],
        teamCode: [
          { required: true, message: '请选择生产队', trigger: 'blur' },
        ],
        personPhone: [
          { required: true, message: '请选择押车人', trigger: 'change' },
        ],
        massifNumber: [
          { required: true, message: '请选择地块号', trigger: 'change' },
        ],
        cropType: [
          { required: true, message: '请选择种类', trigger: 'blur' },
        ],
        cropVarieties: [
          { required: true, message: '请选择作物品种', trigger: 'blur' },
        ],
        carName: [
          { required: true, message: '请输入车牌号', trigger: 'blur' },
        ],
        warehouseNumber: [
          { required: true, message: '请选择仓号', trigger: 'blur' },
        ],
        stampId: [
          { required: true, message: '请选择垛号', trigger: 'blur' },
        ],
        categoryName: [
          { required: true, message: '请选择类别', trigger: 'blur' },
        ],
        sourceTeam: [
          { required: true, message: '请选择生产队', trigger: 'blur' },
        ],

      },
      // 车牌号
      carList: [],
      // 司机
      driverList: [],
      escortPersonList: [],
      // 地块号
      massifNumberList: [],
      // 农场
      farmList: [],
      // 生产队
      teamList: [],
      // 作物种类
      cropinfoList: [],
      // 作物品种
      cropVarietiesList: [],
      // 作物类别
      categoryList: [],
      // 品质
      cropQualityList: [],
      // 仓号
      wareList: [],
      // 垛号
      duoList: [],
      carInfo: false,
      driverInfo: false,
      betcarInfo: false,
      cropInfo: false,
      cropVariety: false,
      cropType: false,
      waterStandard: false,
      impurityStandard: false,
      carTypeInfo: 0,
      timeOutFun: null,
      timeOutFun1: null,
      vehicleInfoId: "",
      dialogVisible: false,
      innerVisible: false,
      innerVisible2: true,
    };
  },
  computed: {
    ...mapGetters(['hasIdentifier']), // 映射 getters
  },
  methods: {
    radioClick() {
      this.ruleForm.radio = true;
      this.ruleForm2 = this.ruleForm;
      this.ruleForm = {
        ...this.ruleForm,
        driverName: "",
        personName: "",
        teamCode: "",
        sourceTeam: "",
        warehouseNumber: "",
        cropQuality: "",
        stampId: "",
        cropType: "",
        cropVarieties: "",
        categoryName: "",
        id: ''
      }
      this.setAA()
    },
    radioChange() {
      if (this.ruleForm.radio) {
        this.ruleForm2 = this.ruleForm;
        this.ruleForm = {
          ...this.ruleForm,
          driverName: "",
          personName: "",
          sourceTeam: "",
          warehouseNumber: "",
          cropQuality: "",
          stampId: "",
          cropType: "",
          cropVarieties: "",
          categoryName: "",
          id: ''
        }
        // this.ruleForm.radio=!this.ruleForm.radio;
      } else {
        this.ruleForm = { ...this.ruleForm2 }
        this.ruleForm.radio = false;
      }
      this.setAA()
    },
    addTableDate(data) {
      this.ruleForm.carNameBak = this.ruleForm.carName
      this.ruleForm.carName = data;
      this.innerVisible2 = true
      this.ruleForm = {
        ...this.ruleForm,
        driverName: "",
        personName: "",
        sourceTeam: "",
        warehouseNumber: "",
        cropQuality: "",
        stampId: "",
        cropType: "",
        cropVarieties: "",
        categoryName: "",
        id: ''
      }
      this.setAA('cass')
      clearTimeout(this.timeOutFun1)
    },
    driverNamechange(e) {
      let i = this.driverList.findIndex(item => item.value == e)
      this.ruleForm.driverName = this.driverList[i].label
    },
    // 选择押车人 escortPersonList
    handleEscortPerson(e) {
      let i = this.escortPersonList.findIndex(item => item.value == e)
      this.ruleForm.personName = this.escortPersonList[i].label
    },
    // 选择地块号
    async handleChangeMassifNumber() {
      if (this.ruleForm.sourceTeam) {
        this.$set(this.ruleForm, 'massifNumber', '')
        this.massifNumberList = []
        let res = await getPlotUnitListApi({ teamId: this.ruleForm.sourceTeam })
        this.massifNumberList = res.data.map(item => {
          return {
            label: item.massifName,
            value: item.massifNumber
          }
        })
      }
    },
    async handleChangeMassifNumberEdit() {
      if (this.ruleForm.sourceTeam) {
        let res = await getPlotUnitListApi({ teamId: this.ruleForm.sourceTeam })
        this.massifNumberList = res.data.map(item => {
          return {
            label: item.massifName,
            value: item.massifNumber
          }
        })
      }
    },
    setmassifName(e) {
      let i = this.massifNumberList.findIndex(item => item.value == e)
      this.ruleForm.massifName = this.massifNumberList[i].label
    },
    handleClose() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        farmCode: this.$farmCode,
        teamCode: this.$teamCode,
        radio: false,
        carName: ''
      }
      clearTimeout(this.timeOutFun)
      clearTimeout(this.timeOutFun1)
      this.innerVisible2 = true
      this.$refs.ruleForm.resetFields();
      this.$emit('handlegetCarNumber')
    },
    // 提交
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.tare == 0) {
            //空车
            addApi({ ...this.ruleForm, nw: "", type: '2' }).then((res) => {
              if (this.ruleForm.tare && (this.ruleForm.gw * 1) <= (this.ruleForm.tare * 1)) {
                Message({
                  message: '毛重值应大于皮重值',
                  type: 'error',
                  duration: 3 * 1000
                })
                // this.ruleForm.tare = ''
              } else {
                this.handleClose();
                this.$emit('handleRefresh')
              }

            })
          } else {
            if (this.ruleForm.radio) {
              addApi({ ...this.ruleForm, id: "", nw: "", type: '2' }).then((res) => {
                if (this.ruleForm.tare && (this.ruleForm.gw * 1) <= (this.ruleForm.tare * 1)) {
                  Message({
                    message: '毛重值应大于皮重值',
                    type: 'error',
                    duration: 3 * 1000
                  })
                  // this.ruleForm.tare = ''
                } else {
                  this.handleClose();
                  this.$emit('handleRefresh')
                }
              })
            } else {
              editApi({ ...this.ruleForm, type: '2',nwOne:this.netWeight }).then((res) => {
                if (this.ruleForm.tare && (this.ruleForm.gw * 1) <= (this.ruleForm.tare * 1)) {
                  Message({
                    message: '毛重值应大于皮重值',
                    type: 'error',
                    duration: 3 * 1000
                  })
                  // this.ruleForm.tare = ''
                } else {
                  this.handleClose();
                  this.$emit('handleRefresh')
                }
              })
            }
          }
        } else {
          return false;
        }
      });
    },
    // 获取下拉框
    handleGetSelect() {
      Promise.all([
        carinfoApi(this.$farmCode),
        driverInfoApi(this.$farmCode),
        selectApi({ type: 'farm_type' }),
        categorySelectApi(),
        escortPersonApi(this.$farmCode),
      ]).then(resList => {
        this.carList = filterSelect(resList[0].data)
        this.driverList = resList[1].data
        if (this.$status != '0') {
          this.ruleForm.farmCode = this.$farmCode
          this.farmList = resList[2].data.dictItemList.filter(item => item.value == this.$farmCode)
          if (this.ruleForm.id) {
            // this.handleGetCropTypeEdit()
          } else {
            this.handleGetCropType()
          }
          this.handleSelectTeam(this.$farmCode)
        } else {
          this.farmList = resList[2].data.dictItemList
        }
        this.categoryList = filterSelect(resList[3].data)
        this.escortPersonList = resList[4].data
        // this.handleGetCropType()
        this.handleWarehouse()

      })
    },
    // 根据生产队选择种类  修改回填
    async handleGetCropTypeEdit() {
      let res = await getCropTypeApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode })
      this.cropinfoList = res.data
      this.handleChangeCropTypeEdit(this.ruleForm.cropType)
      this.handleChangeMassifNumberEdit()
    },
    // 选择生产队
    async handleSelectTeam(e) {
      let i = this.farmList.findIndex(item => item.value == e)
      this.ruleForm.remarks = this.farmList[i].remarks
      let res = await selectApi({ type: e })
      this.teamList = res.data.dictItemList
    },
    // 根据生产队选择种类
    async handleGetCropType() {
      // this.cropinfoList = []
      // this.ruleForm.cropType = ''
      // this.ruleForm.cropQuality = ''
      // this.ruleForm.warehouseNumber = ''
      // this.ruleForm.stampId = ''
      if (this.ruleForm.farmCode && this.ruleForm.sourceTeam) {
        this.handleChangeMassifNumber()
        let res = await getCropTypeApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode })
        this.cropinfoList = res.data
      }
    },
    // 检验种类是否有值
    handleInspect() {
      if (!this.cropinfoList.length) {
        Message({
          message: '暂无作物种类，请前往填报',
          type: 'warning',
          duration: 3 * 1000
        })
      }
    },
    // 选择作物品种
    async handleChangeCropType(e) {
      this.ruleForm.cropQuality = ''
      this.ruleForm.warehouseNumber = ''
      this.ruleForm.stampId = ''
      if (!e) return
      Promise.all([
        getQualityApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType }),
        cropTypeApi(e)
      ]).then(res => {
        this.cropQualityList = filterSelect(res[0].data)
        this.cropVarietiesList = filterSelect(res[1].data)
      })

    },
    // 选择作物品种
    async handleChangeCropTypeEdit(e) {
      Promise.all([
        getQualityApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType }),
        cropTypeApi(e)
      ]).then(res => {
        this.cropQualityList = filterSelect(res[0].data)
        this.cropVarietiesList = filterSelect(res[1].data)
      })

    },
    // 获取仓号
    async handleWarehouse() {
      this.ruleForm.warehouseNumber = ''
      this.ruleForm.stampId = ''
      if (this.ruleForm.farmCode && this.ruleForm.teamCode && this.ruleForm.cropType && this.ruleForm.cropQuality) {
        let res = await warehouseApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType, cropQuality: this.ruleForm.cropQuality })
        if (res.code == 0) {
          this.wareList = res.data
        } else {
          this.wareList = []
          this.ruleForm.warehouseNumber = ''
        }
      }
    },
    // 二次回显
    async handleWarehouseEdit() {
      let res = await warehouseApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType, cropQuality: this.ruleForm.cropQuality })
      if (res.code == 0) {
        this.wareList = res.data
      } else {
        this.wareList = []
        this.ruleForm.warehouseNumber = ''
      }
      this.handleDuoEdit()
    },
    // 选择垛号
    async handleDuo() {
      this.ruleForm.stampId = ''
      if (this.ruleForm.farmCode && this.ruleForm.teamCode && this.ruleForm.cropType && this.ruleForm.cropQuality && this.ruleForm.warehouseNumber) {
        this.ruleForm.warehouseId = this.wareList.find(item => item.label == this.ruleForm.warehouseNumber).value
        let res = await getduoApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType, cropQuality: this.ruleForm.cropQuality, wareId: this.ruleForm.warehouseId })
        this.duoList = filterSelect(res.data)
      }
    },
    async handleDuoEdit() {
      this.ruleForm.warehouseId = this.wareList.find(item => item.label == this.ruleForm.warehouseNumber).value

      let res = await getduoApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType, cropQuality: this.ruleForm.cropQuality, wareId: this.ruleForm.warehouseId })
      this.duoList = filterSelect(res.data)
    },
    // 毛重
    handleGW() {
      if (this.ruleForm.gw) {
        this.ruleForm.gw = (this.ruleForm.gw * 1).toFixed(2)
      }
      if (this.ruleForm.tare && (this.ruleForm.gw * 1) > (this.ruleForm.tare * 1)) {
        this.ruleForm.nw = (this.ruleForm.gw - this.ruleForm.tare).toFixed(2)
        this.netWeight = this.ruleForm.nw
        this.handleWater()
        this.handleMis()
      } else if (this.ruleForm.tare && (this.ruleForm.gw * 1) <= (this.ruleForm.tare * 1)) {
        Message({
          message: '毛重值应大于皮重值',
          type: 'error',
          duration: 3 * 1000
        })
        // this.ruleForm.gw = ''
      }
    },
    // 皮重
    handleTARE() {
      if (this.ruleForm.tare) {
        this.ruleForm.tare = (this.ruleForm.tare * 1).toFixed(2)
      }
      if (this.ruleForm.gw && (this.ruleForm.gw * 1) > (this.ruleForm.tare * 1)) {
        if (this.ruleForm.tare * 1 != 0) {
          this.ruleForm.nw = (this.ruleForm.gw - this.ruleForm.tare).toFixed(2)
          this.netWeight = this.ruleForm.nw
          this.handleWater()
          this.handleMis()
        }

      } else if (this.ruleForm.tare && (this.ruleForm.gw * 1) <= (this.ruleForm.tare * 1)) {
        Message({
          message: '毛重值应大于皮重值',
          type: 'error',
          duration: 3 * 1000
        })
        // this.ruleForm.tare = ''
      }
    },
    // 扣水
    handleWater() {
      this.waterWeight = (this.netWeight * ((this.ruleForm.waterRatio * 1) / 100)).toFixed(2)
      this.ruleForm.nw = (this.netWeight - (this.netWeight * ((this.ruleForm.waterRatio * 1) / 100)) - (this.netWeight * ((this.ruleForm.mixedRatio * 1) / 100))).toFixed(2)
    },
    // 扣杂
    handleMis() {
      this.mixedWeight = (this.netWeight * ((this.ruleForm.mixedRatio * 1) / 100)).toFixed(2)
      this.ruleForm.nw = (this.netWeight - (this.netWeight * ((this.ruleForm.waterRatio * 1) / 100)) - (this.netWeight * ((this.ruleForm.mixedRatio * 1) / 100))).toFixed(2)
    },
    // 新增弹框
    handleShowAddDialog(val) {
      if (val === 'carInfo') {
        this.$refs.CarInfo.handleGetInfo()
        this.$refs.CarInfo.handleGetSelect()
        this.carInfo = true
      }
      if (val === 'driverInfo') {
        this.$refs.DriverInfo.handleGetInfo()
        this.$refs.DriverInfo.handleGetSelect()
        this.driverInfo = true
      }
      if (val === 'betcarInfo') {
        this.$refs.BetcarInfo.handleGetInfo()
        this.betcarInfo = true
      }
      if (val === 'cropInfo') {
        this.$refs.CropInfo.handleGetInfo()
        this.cropInfo = true
      }
      if (val === 'cropVariety') {
        this.$refs.CropVariety.handleGetInfo()
        this.$refs.CropVariety.handleGetSelect()
        this.cropVariety = true
      }
      if (val === 'cropType') {
        this.$refs.CropType.handleGetInfo()
        this.cropType = true
      }
      if (val === 'waterStandard') {
        this.$refs.WaterStandard.handleGetInfo()
        this.$refs.WaterStandard.handleGetFarm()
        this.$refs.WaterStandard.handleGetSelect()
        this.waterStandard = true
      }
      if (val === 'impurityStandard') {
        this.$refs.ImpurityStandard.handleGetInfo()
        this.$refs.ImpurityStandard.handleGetFarm()
        this.$refs.ImpurityStandard.handleGetSelect()
        this.impurityStandard = true
      }
      if (val == 'editCarNumber') {

        this.$refs.handlTableAdd.titleText = this.ruleForm.carNameAA
        this.dialogVisible = true
      }
    },
    // 更新下拉框
    async handleRefreshList(val) {
      if (val === 'carInfo') {
        let res = await carinfoApi(this.$farmCode)
        this.carList = filterSelect(res.data)
      }
      if (val === 'driverInfo') {
        let res = await driverInfoApi(this.$farmCode)
        this.driverList = res.data
      }
      if (val === 'betcarInfo') {
        let res = await escortPersonApi(this.$farmCode)
        this.escortPersonList = res.data
      }
      if (val === 'cropInfo') {
        let res = await cropinfoSelectApi()
        this.cropinfoList = res.data
      }
      if (val === 'cropVariety') {
        if (!this.ruleForm.cropType) return
        let res = await cropTypeApi(this.ruleForm.cropType)
        this.cropVarietiesList = filterSelect(res.data)
      }
      if (val === 'cropType') {
        let res = await categorySelectApi()
        this.categoryList = filterSelect(res.data)
      }
      if (val === 'waterStandard') {

      }
      if (val === 'impurityStandard') {
      }

    },
    // 单次获取毛重
    getTotalWeight() {
      getInboundByCarName({ carName: this.ruleForm.carName, farmCode: this.$farmCode }).then(ress => {
        getCropWeight({farmCode: this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
          if (ress.data === null) {
            this.ruleForm.gw = res.data.data;
          } else {
            this.ruleForm.gw = ress.data.gw;
            this.ruleForm.tare = res.data.data;
            console.log('表单', this.ruleForm);
          }
        })

      })

    },
    // 单次获取车牌号
    getCarName() {
      MessageBox.confirm('刷新后，将丢失本次车牌号记录，重新获取车牌号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'cancel',
        type: 'warning'
      }).then(() => {
        refreshWithApi({ carNumber: this.ruleForm.carName }).then(res => {
          this.ruleForm.carName = res.data.data
          if (res.data.data) {
            this.innerVisible2 = true
            this.setAA('cass')
          }
        })
      })
      // getCarLicense().then((res) => {
      //   this.ruleForm = {
      //     ...this.ruleForm,
      //     carName: res.data.data
      //   }
      //   this.innerVisible2 = true
      //   this.setAA('cass')
      // })
    },
    // async handleGetCropType() {
    //   this.cropinfoList = []
    //   if (!this.ruleForm.farmCode || !this.ruleForm.teamCode) return
    //   let res = await getCropTypeApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode })
    //   this.cropinfoList = res.data
    // },
    setSelectList() {

      this.handleGetCropTypeEdit()
      // this.handleChangeCropType(this.ruleForm.cropType);
      // warehouseApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType, cropQuality: this.ruleForm.cropQuality }).then((res)=>{
      //   if (res.code == 0) {
      //     this.wareList = res.data
      //     if (this.ruleForm.farmCode && this.ruleForm.teamCode && this.ruleForm.cropType && this.ruleForm.cropQuality && this.ruleForm.warehouseNumber) {
      //       this.ruleForm.warehouseId = this.wareList.find(item => item.label == this.ruleForm.warehouseNumber).value
      //       getduoApi({ farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropType: this.ruleForm.cropType, cropQuality: this.ruleForm.cropQuality, wareId: this.ruleForm.warehouseId }).then((res)=>{
      //         this.duoList = filterSelect([res.data])
      //       })
      //     }
      //   } else {
      //     this.wareList = []
      //     this.ruleForm.warehouseNumber = ''
      //   }
      // })

    },
    // 映射之前数据
    mappingRuleForm(obj) {
      this.ruleForm = {
        // ...this.ruleForm,
        ...obj,
        tare: this.ruleForm.gw,
        nw: (obj.gw * 1 - this.ruleForm.gw * 1) * 1,
        peelingTime: formatDate(new Date()),
      };
      this.netWeight = this.ruleForm.nw
      // this.handleTARE()
      // this.setSelectList()
      this.farmList = []
      this.handleGetCropTypeEdit()
      this.handleWarehouseEdit()


      // setTimeout(() => {

      // },10);

    },
    // 连续获取毛重
    getZl() {
      clearTimeout(this.timeOutFun)
      getCropWeight({farmCode: this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
        this.ruleForm.gw = res.data.data
        this.timeOutFun = setTimeout(() => {
          this.getZl()
        }, 3000)
      })
    },
    getZl2() {
      clearTimeout(this.timeOutFun)
      getCropWeight({farmCode: this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
        this.ruleForm.tare = res.data.data
        this.timeOutFun = setTimeout(() => {
          this.getZl2()
        }, 3000)
      })
    },
    setAA(aa) {
      getInboundByCarName({ carName: this.ruleForm.carName, farmCode: this.$farmCode }).then((ress) => {
        if (ress.data === null) {
          // this.ruleForm.tare=0;
          this.ruleForm = {
            ...this.ruleForm,
            tare: '0',
            // nw:this.ruleForm.gw,
            hairingTime: formatDate(new Date())
          }
          this.getZl()
          this.handleTARE()
        } else {
          // if(!this.ruleForm.tare){
          //   this.mappingRuleForm(ress.data)
          // }
          // if(this.innerVisible2 &&this.ruleForm.nw<500 &&this.ruleForm.tare!=0){
          //   this.innerVisible=true
          // }
          if (this.ruleForm.radio) {
            this.getZl()
            this.ruleForm = {
              ...this.ruleForm,
              tare: '0',
              // nw:this.ruleForm.gw,
              hairingTime: formatDate(new Date()),
              peelingTime: formatDate(new Date())
            }
          } else {
            this.getZl2()
            //
            if (!this.ruleForm.tare) {
              setTimeout(() => {
                this.mappingRuleForm(ress.data)
              })
            }
            if (aa) {
              setTimeout(() => {
                this.mappingRuleForm(ress.data)
              })
            }
            this.ruleForm = {
              ...this.ruleForm,
              // ...obj,
              nw: ress.data.gw * 1 - this.ruleForm.gw * 1,
              peelingTime: formatDate(new Date())
            };
            setTimeout(() => {
              if (this.innerVisible2 && this.ruleForm.nw < 500 && this.ruleForm.tare * 1 != 0) {
                this.innerVisible = true
              }
            })

          }
        }

      })
    },
    // 连续获取车牌号、之前数据
    getCarNumber() {
      clearTimeout(this.timeOutFun1)
      getCarLicense({farmCode:this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
        if (res.data.data == '') {
          this.timeOutFun1 = setTimeout(() => {
            this.getCarNumber()
          }, 3000)
        } else {
          this.ruleForm.carName = res.data.data
          this.ruleForm.carNameAA = res.data.data
          this.setAA()
        }
      })
    }
  },
  watch: {
    'ruleForm.gw': function (a, b) {
      this.handleTARE();
    },
    'ruleForm.tare': function (a, b) {
      this.handleTARE();
    },
  }
}
</script>

<style lang='scss' scoped>
.error-message {
  color: glay;
  font-size: 12px;
  margin-top: 5px;
}

.form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.el-form-item {
  width: 33%;
  margin-bottom: 12px;

}

.fir-title {
  margin-top: 0 !important;
}

.content {
  display: flex;
  padding: 0;
}

.fir-title {
  margin-top: 0 !important;
  display: flex;
  line-height: 55px;
}


.button-box {
  border-radius: 5px;
  margin: 0;
  padding: 0;
  margin-left: 1240px;
  line-height: 30px;

  .button-boxItem {
    background: #0e7b67;
    cursor: pointer;
    padding: 0 15px;
    height: 40px;
    line-height: 40px;
    border-radius: 20px;
    color: #fff;
  }
}

.inStoragePCButton {
  width: 90px;
  height: 55px;
  position: relative;
  float: left;
  background: #0e7b67;
  color: #fff;
  font-weight: 700;
  font-size: 1.2vw;
  line-height: 55px;
  text-align: center;
  // margin: 2vh 1vw;
  border-radius: 5px;
  cursor: pointer;
}

.inStoragePCButton2 {
  width: 90px;
  height: 55px;
  position: relative;
  float: left;
  background: pink;
  color: #fff;
  font-weight: 700;
  font-size: 1.2vw;
  line-height: 55px;
  text-align: center;
  // margin: 2vh 1vw;
  border-radius: 5px;
  cursor: pointer;
}


::v-deep .el-form-item__content {
  display: flex;
  align-items: center;
  height: 36px;
  line-height: 36px;
}

::v-deep .el-input {
  flex: 1;
  height: 36px;
}

::v-deep .el-select {
  flex: 1;
  height: 36px;
}

::v-deep .el-input__icon {
  line-height: 36px;
}

::v-deep .el-input__inner {
  height: 36px;
}

.activeClassInput {
  ::v-deep .el-input__inner {
    color: rgb(92, 112, 234) !important;
    height: 36px;
    font-weight: 900;
    font-size: 17px;
  }

  ::v-deep .el-input--suffix {
    font-size: 17px;
    color: rgb(92, 112, 234) !important;
    height: 36px;
    font-weight: 900;
  }

  ::v-deep .el-input {
    width: 100%;
  }
}

.title {
  color: #000;
  font-size: 17px !important;
  font-weight: bold;
  // margin-bottom: 0px;
  margin: 5px 0 !important;

}

.dialog-footer333 {
  position: absolute;
  width: 180px;
  left: 50%;
  bottom: 3%;
  transform: translateX(-50%);
}
</style>
